from ..globals import g
from .common import execute
from ..locator import locating_element
from ..elements import e
from ..log import logger
from ..windows import w


class TestData:
    @staticmethod
    def update(d):
        if not isinstance(d, dict):
            raise TypeError(f'参数类型[{type(d)}]错误，请使用字典类型参数！')
        g.var.update(d)

    @staticmethod
    def set(key, value):
        g.update_var(key, value)

    @staticmethod
    def get(key, default=None):
        return g.get_var(key, default)

    def __getitem__(self, item):
        return g.get_var(item)


testdata = TestData()
web_driver = g.driver


def run_snippet():
    step = {
        'snippet_setting': {}
    }
    execute(step={})


class Element:
    def __init__(self, el_id):
        self.el_id = el_id
        w.switch_frame(e.get(el_id)[0])

    def locate(self):
        return locating_element(self.el_id)

    def click(self):
        locating_element(self.el_id).click()

    def send_keys(self, s):
        locating_element(self.el_id).send_keys(s)

    def __getattr__(self, attr):
        """
        Attrs:
            by
            value
        """
        ele, value = e.get(self.el_id)
        v = ele.get(attr, None)
        if not v:
            raise AttributeError(f'没有<{attr}>属性')
        return v


class DataBase:
    def __init__(self):
        pass

